import json

import numpy as np
import pandas as pd
from django.http import HttpResponse

from .utils.token import create_token





def typecount(request):
    data = pd.read_csv('mysite/data/data.csv')
    type_list = set(list(data['类型']))  # 获取古诗类型集合
    type_group = data.groupby(by='类型')  # 数据按类型分组
    type_count = np.linspace(0, 0, len(type_list)).astype(int)  # 列表：用来存各个类型的古诗数量
    index = 0
    print('【一、按类型分析：】')
    for t in type_list:  # 遍历每种类型的古诗
        data_type = type_group.get_group(t)
        type_count[index] = len(data_type)  # 该类型的古诗数量
        print(" " + t + "：" + str(type_count[index]) + " 首")
        index += 1
    type_count=type_count.tolist()
    type_list=list(type_list)
    # json格式
    result={"唐诗类别":type_list,"数量":type_count}
    json_data=json.dumps(result,ensure_ascii=False)
    return HttpResponse(json_data,content_type="application/json,charset=utf-8")


def top10(request):
    data = pd.read_csv('mysite/data/data.csv')
    author_list = set(list(data['作者']))  # 获取古诗捉着集合
    author_group = data.groupby(by='作者')  # 数据按作者分组
    author_count = np.linspace(0, 0, len(author_list)).astype(int)  # 列表：用来存各个作者的古诗数量
    index = 0
    print('【二、按作者分析：】')
    for a in author_list:  # 遍历每种类型的古诗
        data_type = author_group.get_group(a)
        author_count[index] = len(data_type)  # 该类型的古诗数量
        # print(" " + a + "：" + str(author_count[index]) + " 首")
        index += 1
    author_poem = pd.DataFrame()
    author_poem['作者'] = list(author_list)
    author_poem['数量'] = list(author_count)
    author_poem = author_poem.sort_values(by='数量', ascending=False)  # 排序
    author_list = []
    author_count = []
    # 作者数量太多，只取前十
    for i in range(0, 10):
        author_list.append(list(author_poem['作者'])[i])
        author_count.append(list(author_poem['数量'])[i])
    # json格式
    result = {"作者": author_list, "数量": author_count}
    json_data = json.dumps(result, ensure_ascii=False)
    return HttpResponse(json_data, content_type="application/json,charset=utf-8")

#写一个作者列表，点击作者，可以显示对应的饼图
def author_pom_type(request):
    data = pd.read_csv('mysite/data/data.csv')
    # 初始化一个字典，用于存储每个作者的古诗类型和数量
    author_poem_type_count = {}

    # 按作者分组
    author_group = data.groupby(by='作者')

    # 遍历每个作者
    for author, author_data in author_group:
        # 获取该作者的古诗类型集合
        type_list = set(author_data['类型'])
        # 初始化一个字典，用于存储该作者的古诗类型和数量
        author_type_count = {}
        # 按类型分组
        type_group = author_data.groupby(by='类型')
        # 遍历每种类型的古诗
        for t in type_list:
            data_type = type_group.get_group(t)
            author_type_count[t] = len(data_type)  # 该类型的古诗数量
        # 将该作者的古诗类型和数量存储到大字典中
        author_poem_type_count[author] = author_type_count

        # json格式
    result = {"作者唐诗类别数量": author_poem_type_count}
    json_data = json.dumps(result, ensure_ascii=False)
    return HttpResponse(json_data, content_type="application/json,charset=utf-8")

# 输入作者和唐诗
def pom(request):
    data = pd.read_csv('mysite/data/data.csv')
    # 唐诗的作者和标题作为键，内容作为值
    poem_dict={}
    # 遍历数据
    for index, row in data.iterrows():
        # 构造键：作者 + 标题
        author=row['作者']
        title=row['标题']
        key = f'{author}'
        # 获取内容
        value =f'{title}'
        content=row['正文']
        if key not in poem_dict:
            poem_dict[key] = []
        # 将键值对存入字典
        poem_dict[key].append({value:content})

    # json格式
    result = {"content": poem_dict}
    json_data = json.dumps(result, ensure_ascii=False)
    return HttpResponse(json_data, content_type="application/json,charset=utf-8")

def content(request):
    data = pd.read_csv('mysite/data/data.csv')
    content=data['正文']
    # json格式
    result = {"pom": content}
    json_data = json.dumps(result, ensure_ascii=False)
    return HttpResponse(json_data, content_type="application/json,charset=utf-8")




def login(request):
    username = request.GET.get('username')
    password = request.GET.get('password')
    if username is None:
        result = {"error": "请输入用户名"}
        return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json,charset=utf-8")
    if password is None:
        result = {"error": "请输入密码"}
        return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json,charset=utf-8")
    token=create_token(username,password)
    result = {"success": "登陆成功","token": token}
    return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json,charset=utf-8")





